/*
 * uart.c
 *
 *  Created on: May 8, 2022
 *      Author: Abyss
 */
#include "config.h"

 //串口1重定向
 #ifdef __GNUC__
 #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
 #else
 #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
 #endif
 PUTCHAR_PROTOTYPE
 {
 		HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1,1000);
 		return ch;
 }


 //等待串口数据超时
 int wait(char str[],uint32_t timeout)
 {

 	buffer_flag = FLAG_FLASE;
 	//锁存连接状态
 	int this_tc = _TC;
 	//将连接状态断开 防止htim17中断对数据进行拦截  获取数据解析权
 	 _TC = TC_DISCONNECT;

 	int this_tim = sys_time;
 	//等待超时
 	while(sys_time<timeout+this_tim)
 	{
 		if(buffer_flag == FLAG_TRUE)
 		{
			 //打印串口1方便调试
			// HAL_UART_Transmit(&huart1, buffers, buffer_cnt, 1000);

 			 buffer_flag = FLAG_FLASE;
 			 buffer_cnt = 0;

 			 //判断是否是str
 			 if(strstr(buffers,str)!=0)
 			 {
 				 //恢复htim17处理串口数据
 				_TC = this_tc;
 				 return AT_OK;
 			 }

 		}
 	}
 	 //恢复htim17处理串口数据
 	_TC = this_tc;
 	 buffer_flag = FLAG_FLASE;
 	 buffer_cnt = 0;
 	printf("wait %s is TimeOut\r\n",str);
 	return AT_TIMOUT;
 }


//发送at指令
 int at_send(char *at,uint32_t timeout)
 {
	 uint8_t cmd[1024] = {"\0"};
	 //复制命令
	 strcat(cmd,at);

	 //判断换行符
	 if(strstr(cmd,"\r\n")==0)
	 {
		 strcat(cmd,"\r\n");
	 }
	 //发送命名
	 HAL_UART_Transmit(&huart2, cmd, sizeof(cmd), 1000);




	 if(wait("OK",timeout)==AT_OK)
	 {
		 return AT_OK;
	 }
	 else
	 {
		 return AT_TIMOUT;
	 }
	 return AT_FAIL;

 }

 //softap配网
 int wifi_config()
 {
	 WS281x_SetRGB(CYAN);
	//断开mqtt连接
	if(at_send("AT+TCMQTTDISCONN\r\n",2000)==AT_OK)
	{
		printf("AT+TCMQTTDISCONN is ok\r\n");
	}
	 //断开与 AP 的连接  为配网做准备
	 _TC = TC_DISCONNECT;
 	if(at_send("AT+CWQAP\r\n",2000)==AT_OK)
 	{
 		printf("AT+CWQAP is ok\r\n");
 	}
 	//以 softAP 方式进行 Wi-Fi 配网及设备绑定
 	if(at_send("AT+TCSAP=\"esp12f_jiangxiaojian\"\r\n",5000)==AT_OK)
 	{
 		printf("AT+TCSAP is ok\r\n");
 	}

 	 buffer_flag = FLAG_FLASE;
 	 sys_time = 0;

 	 if(wait("SUCCESS",1000*150)==AT_OK)
 	 {
			printf("SoftAP is ok\r\n");
			 //配置 MQTT 连接参数
			 if(at_send("AT+TCMQTTCONN=1,5000,240,0,1\r\n",2000)==AT_OK)
			 {
				 printf("AT+TCMQTTCONN is ok\r\n");
			 }

			 char data[256]={"\0"};
			 sprintf(data,"AT+TCMQTTSUB=\"$thing/down/property/%s/%s\",0\r\n",_ID,_NAME);

			 if(at_send(data,4000)==AT_OK)
			 {
				 printf("AT+TCMQTTSUB is ok\r\n");
				 _TC = TC_CONNECT;
			 }
			 free(data);

			 return AT_OK;
 	 }
 	 else
 	 {

 	 	//退出 softAP 方式 Wi-Fi 配网状态
 	 	if(at_send("AT+TCSTOPSAP\r\n",5000)==AT_OK)
 	 	{
 	 		printf("AT+TCSTOPSAP is ok\r\n");
 	 	}
 		return AT_TIMOUT;
 	 }
 }


//串口回调
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
     UNUSED(huart);
	 if(huart == &huart2)
	 {
		    //开启定时器计时  3.5没接收到字符完成一次接收
			__HAL_TIM_SET_COUNTER(&htim17,0);
			HAL_TIM_Base_Start_IT(&htim17);

			//接收缓存
			buffers[buffer_cnt++] = buffer;
			HAL_UART_Receive_IT(&huart2, &buffer, 1);
	 }


	 if(huart == &huart3)
	 {
		  //开启定时器计时  3.5ms没接收到字符完成一次接收
			__HAL_TIM_SET_COUNTER(&htim15,0);
			HAL_TIM_Base_Start_IT(&htim15);

			//接收缓存
			electric[electric_cnt++] = electric_buffer;
			HAL_UART_Receive_IT(&huart3, &electric_buffer, 1);

	 }
 }
